package com.oocl.ita.ui;

import java.awt.Font;
import java.awt.Rectangle;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;

import com.oocl.ita.linstner.chatframe.SendListener;
import com.oocl.ita.linstner.chatframe.ShakeListener;
import com.oocl.ita.linstner.chatframe.ToAllClientsListener;
import com.oocl.ita.linstner.chatframe.ToOneClientListener;
import com.oocl.ita.qqclientt.QQClient;
import com.oocl.ita.sakura.handler.ShakeHandler;

public class ChatFrame extends JFrame {
	private static JList contactJList; // 联系人列表
	private static JTextPane contentTextPane; // 显示聊天内容

	public ChatFrame() {
		init();
		QQClient.setChatFrame(this);
	}

	public void init() {
		// 设置ChatFrame
		setBounds(new Rectangle(100, 100, 663, 596));
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		getContentPane().setLayout(null);
		setResizable(false);

		// 聊天窗口左半部分---------------------------------------------------
		// 聊天标题
		JTextPane jtp1 = new JTextPane();
		jtp1.setBounds(0, 0, 392, 41);
		getContentPane().add(jtp1);
		jtp1.setText("To All");

		// 输入框
		JTextArea jta = new JTextArea();
		jta.setBounds(1, 391, 390, 108);
		getContentPane().add(jta);
		jta.setColumns(10);

		// 装 shake 和 send的按钮 的panel
		JPanel containerpane1 = new JPanel();
		containerpane1.setBounds(0, 499, 392, 47);
		getContentPane().add(containerpane1);
		containerpane1.setLayout(null);

		// 震我一下
		JButton btnShake = new JButton("shake");
		btnShake.setBounds(83, 11, 89, 29);
		containerpane1.add(btnShake);
		btnShake.addActionListener(new ShakeListener());

		// 发送按钮
		JButton btnSend = new JButton("send");
		btnSend.setBounds(237, 11, 89, 29);
		containerpane1.add(btnSend);
		btnSend.addActionListener(new SendListener(jta));

		// 聊天内容的容器
		JScrollPane scrollPane_content = new JScrollPane();
		scrollPane_content.setBounds(0, 41, 392, 350);
		getContentPane().add(scrollPane_content);
		
		// 显示聊天内容
		JTextPane jtp2 = new JTextPane();
		contentTextPane = jtp2;
		jtp2.setEditable(false);
		scrollPane_content.setViewportView(jtp2);
		

		// 聊天内容的ScrollBar
		JScrollBar scrollBar = new JScrollBar();
		scrollBar.setBounds(268, 24, 17, 77);
		getContentPane().add(scrollBar);

		// 聊天窗口 右边部分---------------------------------------------------
		// 装to All 按钮， 和联系人列表
		JPanel panel = new JPanel();
		panel.setBounds(391, 0, 271, 555);
		getContentPane().add(panel);

		// 装toAll 按钮的panel
		JPanel containerpane2 = new JPanel();
		containerpane2.setBounds(0, 0, 271, 114);
		panel.add(containerpane2);
		containerpane2.setLayout(null);
		
		// 改变to方为All的		
		JButton btnToAll = new JButton("to all");
		containerpane2.add(btnToAll);
		btnToAll.setBounds(99, 36, 89, 34);
		panel.setLayout(null);
		btnToAll.addActionListener(new ToAllClientsListener(jtp1));		// toAll 监听

		// 联系人列表prompt
		JTextPane jtp3 = new JTextPane();
		jtp3.setFont(new Font("Tahoma", Font.PLAIN, 15));
		jtp3.setBounds(0, 118, 271, 30);
		jtp3.setEditable(false);
		jtp3.setText(" Users Online");
		panel.add(jtp3);

		// 联系人列表的滚动框
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(0, 148, 271, 396);
		panel.add(scrollPane);

		// 联系人列表
		JList panelist = new JList();
		scrollPane.setViewportView(panelist);
		contactJList = panelist; // 静态变量
		panelist.addListSelectionListener(new ToOneClientListener(panelist, jtp1));		// 注册事件 鼠标更好？


	}

	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {

			public void run() {
				// TODO Auto-generated method stub
				ChatFrame chatFrame = new ChatFrame();
				chatFrame.setVisible(true);
			}
		});

	}

	public static synchronized JList getContactJList() {
		return contactJList;
	}

	public static synchronized void setContactJList(JList contactJList) {
		ChatFrame.contactJList = contactJList;
	}

	public static synchronized JTextPane getContentTextPane() {
		return contentTextPane;
	}

	public static synchronized void setContentTextPane(JTextPane contentTextPane) {
		ChatFrame.contentTextPane = contentTextPane;
	}
	
	
}
